<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<html:header title="盘点单详情">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
        .layui-table-cell {
            height:auto;
            text-overflow:inherit;
            white-space:normal;
            overflow: visible !important;
        }
    </style>

    <script>
        jQuery(function () {
            $(window).focus();
            $("#cancel").click(function () {
                closeSubIframe();
            });
        });


        layui.use(['form', 'table', 'laydate'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate;

            get("${baseUrl}house/checkHouse/getVoById.action?id=${id}", function (res) {
                if (res.code == 1) {
                    $("#print").click(function () {
                        document.getElementById("printFrame").contentWindow.printPage(res.data);
                    });

                    if (res.data.close) {
                        showCloseImage();
                    }

                    $("#no").html(res.data.no);
                    $("#house").html(res.data.houseVo.store.name + ">" + res.data.houseVo.name);
                    $("#user").html(res.data.user.name);
                    $("#date").html(GMTToDate(res.data.date));

                    $("#remark").html(res.data.remark);

                    $("#makeUser").html(res.data.makeUser.name);
                    $("#makeTime").html(res.data.makeTime);

                    let list=[];
                    $.each(res.data.checkHouseGoodsVoList, function (i,d) {
                        let vo = d.goodsVo;
                        vo.goodsId = d.goodsId;
                        vo.number = d.number;
                        vo.stockNum = d.stockNum;
                        vo.addNumber = d.addNumber;
                        vo.delNumber = d.delNumber;
                        vo.addAmount = d.addAmount;
                        vo.delAmount = d.delAmount;
                        vo.remark = d.remark;

                        if (!emptyList(d.checkHouseGoodsSnVoList)) {
                            let addGoodsSnList = [];
                            let fitGoodsSnList = [];
                            let delGoodsSnList = [];
                            $.each(d.checkHouseGoodsSnVoList,function (i,d) {
                                if (d.status == 1) {
                                    fitGoodsSnList.push(d.goodsSn);
                                } else if (d.status == 2) {
                                    addGoodsSnList.push(d.goodsSn);
                                } else if (d.status == 3) {
                                    delGoodsSnList.push(d.goodsSn);
                                }

                            });
                            vo.addGoodsSnList = addGoodsSnList;
                            vo.delGoodsSnList = delGoodsSnList;
                            vo.fitGoodsSnList = fitGoodsSnList;
                        }

                        list.push(vo);
                    });
                    renderTable(list);
                }else {
                    tip("获取订单信息失败");
                }
            });
        });

        function renderTable(list) {
            layui.table.render({
                id:'table',
                elem: '#table',
                data:list,
                totalRow:true,
                text: {
                    none: '请选择商品' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                cols: [[
                    {field:'index', width:70,title:"序号",templet:function (d) {
                            if (d.number > d.stockNum) {
                                return '<span class="layui-badge layui-bg-green">盘盈</span>';
                            } else if (d.number < d.stockNum) {
                                return '<span class="layui-badge layui-bg-orange">盘亏</span>';
                            } else {
                                return d.LAY_INDEX;
                            }
                        }},
                    {field: 'name', minWidth: 140, title: '名称/规格',style:"font-size:12px;",templet:function (d) {
                            return genGoodsName(d.name,d.norms)
                        }},
                    {field: 'no', width: 140, title: '商品编号',style:"font-size:12px;"},
                    {field:'stockNum', width: 90, title: '账目数量',templet:function (d) {
                            return getNumber(d.stockNum);
                        }},
                    {field:'number', width: 90, title: '实际数量',templet:"#numberBar"},
                    {width: 120, title: '单位',style:"font-size:12px;", templet:function (d) {
                            return d.goodsId == null ? "" : d.goodsUnitVoList[0].unit.name;
                        }},
                    {field:'snNumberDiv',width: 180, title: '序列号',templet: function (d) {
                            if (d.sn) {
                                if (d.addGoodsSnList == undefined) {
                                    if (emptyList(d.goodsSnList)) {
                                        d.goodsSnList = [];
                                    }
                                    return '<span style="font-size: 12px;">当前当前序列号：</span><a class="snNumberDiv currentSn">'+d.goodsSnList.length+'个</a>';
                                } else {
                                    return '            吻合的序列号：<a class="snNumberDiv fitSn">'+d.fitGoodsSnList.length+'个</a><br>' +
                                        '            盘盈的序列号：<a class="snNumberDiv addSn">'+d.addGoodsSnList.length+'个</a><br>' +
                                        '            盘亏的序列号：<a class="snNumberDiv delSn">'+d.delGoodsSnList.length+'个</a>';
                                }
                            }
                            return '';
                        }},
                    {field:'delNumber', width: 90, title: '盘亏数量',templet:function (d) {
                            return getNumber(d.delNumber);
                        }},
                    {field:'delAmount', width: 90, title: '盘亏金额',templet:function (d) {
                            return getPrice(d.delAmount);
                        }},
                    {field:'addNumber', width: 90, title: '盘盈数量',templet:function (d) {
                            return getNumber(d.addNumber);
                        }},
                    {field:'addAmount', width: 90, title: '盘盈金额',templet:function (d) {
                            return getPrice(d.addAmount);
                        }},
                    {field: 'remark', width: 120, title: '备注',style:"font-size:12px;"},
                ]],
                limit: Number.MAX_VALUE,
                done: function(res, curr, count){
                    $(".currentSn").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table;
                        showSnList(tableData[index].name, tableData[index].goodsSnList);
                    });
                    $(".fitSn").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table;
                        showSnList(tableData[index].name, tableData[index].fitGoodsSnList);
                    });
                    $(".addSn").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table;
                        showSnList(tableData[index].name, tableData[index].addGoodsSnList);
                    });
                    $(".delSn").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table;
                        showSnList(tableData[index].name, tableData[index].delGoodsSnList);
                    });

                    processTotalRow();
                }
            });
        }



        function processTotalRow() {
            let number = 0;
            let stockNum = 0;
            let delNumber = 0;
            let delAmount = 0;
            let addNumber = 0;
            let addAmount = 0;
            $.each(layui.table.cache.table, function (i,d) {
                number += d.number;
                stockNum += d.stockNum;
                delNumber += d.delNumber;
                delAmount += d.delAmount;
                addNumber += d.addNumber;
                addAmount += d.addAmount;
            });
            setTotalRowWithCenter("number",genNumber(number));
            setTotalRowWithCenter("stockNum",genNumber(stockNum));
            setTotalRowWithCenter("delNumber",genNumber(delNumber));
            setTotalRowWithCenter("delAmount",getPrice(delAmount));
            setTotalRowWithCenter("addNumber",genNumber(addNumber));
            setTotalRowWithCenter("addAmount",getPrice(addAmount));
            setTotalRow("0","合计");
        }
    </script>

</html:header>
<iframe id="printFrame" style="width:0px;height:0px" src="${baseUrl}print/checkHousePrint.action" frameborder="0"></iframe>
<body>

<div style="width: 100%; margin-bottom: 20px;overflow: hidden;" >
        <button id="print" class="layui-btn">打印</button>
        <button class="layui-btn layui-btn-primary" id="cancel">取消</button>
</div>

<div class="layuimini-container">
    <div class="layuimini-main">
        <html:closeOrderImage/>

        <h2 class="tableBeforeTitle">商品盘点单</h2>
        <div style="height: 4px;background-color: #bbbbbb;width: 100%;margin: 20px 0 2px 0;"></div>
        <div style="height: 1px;width: 100%;background-color: #dddddd;margin-bottom: 10px;"></div>
        <div style="height: 10px;"></div>

        <form class="layui-form" action="" lay-filter="form">
            <input type="hidden" name="close" value="false">

            <div class="layui-form-item">
                <div class="layui-inline" style="margin:0 20px 10px 0;">
                    <div>
                        <span style="color: #aaaaaa">编号：</span>
                        <span id="no" name="no"></span>
                    </div>
                </div>

                <div class="layui-inline" style="margin:0 20px 10px 0;">
                    <div>
                        <span style="color: #aaaaaa">盘点仓库：</span>
                        <span id="house"></span>
                    </div>
                </div>

                <div class="layui-inline" style="margin:0 20px 10px 0;">
                    <div>
                        <span style="color: #aaaaaa">业务日期：</span>
                        <span id="date"></span>
                    </div>
                </div>

                <div class="layui-inline" style="margin:0 20px 10px 0;">
                    <div>
                        <span style="color: #aaaaaa">经手人：</span>
                        <span id="user"></span>
                    </div>
                </div>
            </div>

            <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>

            <div style='margin: -10px 0 0 0;'>
                <table class="layui-table">
                    <tbody>
                    <tr>
                        <td>
                            备注:<span id="remark"></span>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </form>

        <hr>

        <span style="color: #aaaaaa;">制单人：</span><span id="makeUser"></span>
        <span style="color: #aaaaaa;padding-left: 20px;">制单时间：</span><span id="makeTime"></span>
    </div>

    <script type="text/html" id="numberBar" >
        {{# if(d.sn==true){ }}
        <span class="number">{{ getNumber(d.number) }}</span>
        <span class="numberSn">sn</span>
        {{# }else{ }}
        {{ getNumber(d.number) }}
        {{# } }}
    </script>


</div>

</body>
<html:snListDialog/>
<html:loading/>

<script>
    function openSnCheckHouseDialog(title, url, process) {
        let area;
        if (document.documentElement.clientWidth>600){
            area = ['550px', '560px'];
        }else {
            area = [(document.documentElement.clientWidth-80)+'px', '560px'];
        }

        layer.open({
            title:title,
            area: area,
            skin: 'layui-layer-molv',
            type: 2,
            content: url,
            success: function(layero, index){
                if (process != undefined){
                    process(layero, index);
                }
            }
        });
    }
</script>
</html>